
import { EventTouch, find, Vec2 } from 'cc';
import { UIOpacity, UITransform, Vec3 } from 'cc';
import { tween } from 'cc';
import { _decorator, Component, Label, Node, ProgressBar } from 'cc';
import { App } from '../../App';
import { EventConst } from '../../common/Common';
import UtilTool from '../../tools/UtilTool';
import { Cue } from './Cue';
import { GamePlayState } from './GameBaseLogic';
import { GameBaseModel } from './GameBaseModel';
import { ACTION_POINT_R } from './GameManage';
import { GameModel3 } from './mode3/GameModel3';
const { ccclass, property } = _decorator;

/* 击打作用点调整 */
@ccclass('ActionPoint')
export class ActionPoint extends Component {
    @property(Node)
    contentNode: Node = null

    @property(Node)
    ballbg: Node = null

    @property(Node)
    dialbg: Node = null

    @property(Node)
    redPoint: Node = null

    @property(Node)
    pinNode: Node = null

    @property(Node)
    angleLabel: Node = null

    private touchPos = null   // 触摸位置点
    curType = 0 // 当前操作类型 [0]操作调整旋转 [1]操作弧刻度

    smallDialPos = new Vec3(-350,700,0)
    smallBallPos = new Vec3(-350,-700,0)

    protected onLoad(): void {
    }

    start() {
        UtilTool.setScreen(this.node)
        UtilTool.nodeActive(this.contentNode, 5, 0.16)

        this.ballbg.on(Node.EventType.TOUCH_START, this.touchStart, this);
        this.ballbg.on(Node.EventType.TOUCH_MOVE, this.touchMove, this);
        this.ballbg.on(Node.EventType.TOUCH_CANCEL, this.touchCanel, this);
        this.ballbg.on(Node.EventType.TOUCH_END, this.touchEnd, this);

        this.dialbg.on(Node.EventType.TOUCH_START, this.touchStart_dial, this);
        this.dialbg.on(Node.EventType.TOUCH_MOVE, this.touchMove_dial, this);
        this.dialbg.on(Node.EventType.TOUCH_CANCEL, this.touchCanel_dial, this);
        this.dialbg.on(Node.EventType.TOUCH_END, this.touchEnd_dial, this);

        App.Event.on(EventConst.GAME_HIT_ACTIONVIEW, this.hitView, this)

        this.redPoint.setPosition(App.Model.gameManage.ballActionPoint.x, App.Model.gameManage.ballActionPoint.y)
        this.setShowView(this.curType,true)
        this.setPinNodeAngle(App.Model.gameManage.ballActionDial + 90)
    }



    // 开始点击
    touchStart(event: EventTouch) {
        if (this.curType==1) {
            this.setShowView(0)
            return
        }

        let uiPos = event.getUILocation()
        let nodePos = this.ballbg.getComponent(UITransform).convertToNodeSpaceAR(new Vec3(uiPos.x, uiPos.y, 1))
        
        if (this.judgeValid(nodePos)) {
            this.redPoint.setPosition(nodePos.x, nodePos.y)
            this.setLayerActionPoint(nodePos)
            App.Model.gameManage.setActionPoint(nodePos)
        }
        else{
            // 这里是点击了园球的四个角了，也需要关闭界面
            this.node.destroy()
        }
    }

    // 移动
    touchMove (event: EventTouch) {
        if (this.curType==1) {
            return
        }

        let uiPos = event.getUILocation()
        let nodePos = this.ballbg.getComponent(UITransform).convertToNodeSpaceAR(new Vec3(uiPos.x, uiPos.y, 1))

        if (this.judgeValid(nodePos)) {
            this.redPoint.setPosition(nodePos.x, nodePos.y)
            this.setLayerActionPoint(nodePos)
            App.Model.gameManage.setActionPoint(nodePos)
        }
    }


    // 结束点击
    touchEnd (event: EventTouch) {
        
    }

    // 取消点击
    touchCanel(event: EventTouch) {
        
    }

    /**
     * 判断是否在大球范围内
     */
    judgeValid(pos) {
        let curRadius = UtilTool.getDistance(new Vec2(0, 0), new Vec2(pos.x, pos.y))
        return ACTION_POINT_R >= curRadius
    }   

    /**
     * 设置界面显示
     * @param type 当前操作类型 [0]操作调整旋转 [1]操作弧刻度
     */
    setShowView(type,init=false) {
        this.curType = type
        this.setTip(-1)
        if (init) {
            if (this.curType==0) {
                this.ballbg.setPosition(0,0)
                this.dialbg.setPosition(this.smallDialPos)
            }
            else{
                this.dialbg.setPosition(0,0)
                this.ballbg.setPosition(this.smallBallPos)
            }
            this.setTip(this.curType)
        }
        else{
            if (this.curType==0) {
                tween()
                    .target(this.ballbg)
                    .to(0.3,{ scale: new Vec3(1, 1, 1), position: new Vec3(0, 0, 0) })
                    .call(() => {
                        
                    })
                    .start()

                tween()
                    .target(this.dialbg)
                    .to(0.3,{ scale: new Vec3(0.3, 0.3, 0.3), position: this.smallDialPos })
                    .call(() => {
                        this.setTip(this.curType)
                    })
                    .start()
            }
            else{
                tween()
                    .target(this.dialbg)
                    .to(0.3,{ scale: new Vec3(1, 1, 1), position: new Vec3(0, 0, 0) })
                    .call(() => {
                        
                    })
                    .start()

                tween()
                    .target(this.ballbg)
                    .to(0.3,{ scale: new Vec3(0.3, 0.3, 0.3), position: this.smallBallPos })
                    .call(() => {
                        this.setTip(this.curType)
                    })
                    .start()
            }
        }
    }   

    /**
     * 设置提示
     * @param type 当前操作类型 [0]操作调整旋转 [1]操作弧刻度
     */
    setTip(type) {
        let title = find("title",this.contentNode)
        let title_dial = find("title_dial",this.contentNode)
        let tip = find("tip",this.contentNode)
       
        title.active = type==0
        title_dial.active = type==1
        tip.active = type==0
    }

    /**
     * 设置UI界面上的作用点位置
     */
    setLayerActionPoint(pos) {
        App.Event.emit(EventConst.ACTION_BALL_POINT, pos)
    }   


    /////////////////////////////////////////////// 刻弧度 /////////////////////////////////////////////////////


    // 开始点击
    touchStart_dial(event: EventTouch) {
        if (this.curType==0) {
            this.setShowView(1)
            return
        }

        let uiPos = event.getUILocation()
        let nodePos = this.dialbg.getComponent(UITransform).convertToNodeSpaceAR(new Vec3(uiPos.x, uiPos.y, 1))
        let pinPos = this.pinNode.getPosition()
        let angle = UtilTool.getPointAngle(new Vec2(pinPos.x, pinPos.y), new Vec2(nodePos.x, nodePos.y))
        angle = UtilTool.getRAngle(angle)
        this.setPinNodeAngle(angle)
    }

    // 移动
    touchMove_dial (event: EventTouch) {
        if (this.curType==0) {
            return
        }

        let uiPos = event.getUILocation()
        let nodePos = this.dialbg.getComponent(UITransform).convertToNodeSpaceAR(new Vec3(uiPos.x, uiPos.y, 1))
        let pinPos = this.pinNode.getPosition()
        let angle = UtilTool.getPointAngle(new Vec2(pinPos.x, pinPos.y), new Vec2(nodePos.x, nodePos.y))
        angle = UtilTool.getRAngle(angle)
        this.setPinNodeAngle(angle)
    }

    /**
     * 设置刻针节点的角度
     * @param angle 针到触摸点两点间的角度
     */
    setPinNodeAngle(angle) {
        this.pinNode.angle = this.touchAngleToPin(angle)

        // 设置最终角度数
        let myAngle = this.pinNode.angle + 45
        this.angleLabel.getComponent(Label).string = myAngle + "°"
        // 保存角度数据
        App.Model.gameManage.ballActionDial = myAngle
    }

    /**
     * 触摸点角度转换成弧刻度针角度
     */
    touchAngleToPin(angle) {
        let pinAngle = 0
        if (angle>180) {
            pinAngle = 45
        }
        else if (angle<90) {
            pinAngle = -45
        }
        else{
            pinAngle = Math.floor(angle - 135)
        }

        return pinAngle
    }  

    // 结束点击
    touchEnd_dial (event: EventTouch) {
        
    }

    // 取消点击
    touchCanel_dial(event: EventTouch) {
        
    }


    /**
     * 点击空白处
     */
    clickEmpty() {
        App.Audio.playOneShot("click")
        this.node.destroy()
    }

    /**
     * 关闭界面
     */
    hitView() {
        if (this.node) {
            this.node.destroy()
        }
    }

    onDestroy() {
        App.Event.off(EventConst.GAME_HIT_ACTIONVIEW,this.hitView,this)
    }
}

